import pygame
import random

# 游戏区域的大小
WIDTH = 10
HEIGHT = 20
BLOCK_SIZE = 30

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

# 七种不同的方块形状
SHAPES = [
    [[1, 1, 1, 1]],
    [[1, 1], [1, 1]],
    [[1, 1, 0], [0, 1, 1]],
    [[0, 1, 1], [1, 1, 0]],
    [[1, 0, 0], [1, 1, 1]],
    [[0, 0, 1], [1, 1, 1]],
    [[0, 1, 0], [1, 1, 1]]
]

# 初始化 pygame
pygame.init()

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))
pygame.display.set_caption("Tetris Game")

# 游戏时钟
clock = pygame.time.Clock()

# 初始化游戏板
board = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]


# 生成新的方块
def generate_new_piece():
    shape = random.choice(SHAPES)
    x = WIDTH // 2 - len(shape[0]) // 2
    y = 0
    return shape, x, y


# 检查是否可以移动方块
def is_valid_move(shape, x, y):
    for row in range(len(shape)):
        for col in range(len(shape[row])):
            if shape[row][col] and (x + col < 0 or x + col >= WIDTH or y + row >= HEIGHT or board[y + row][x + col]):
                return False
    return True


# 合并方块到游戏板
def merge_piece(shape, x, y):
    for row in range(len(shape)):
        for col in range(len(shape[row])):
            if shape[row][col]:
                board[y + row][x + col] = 1


# 消除已满的行
def clear_lines():
    full_rows = [row for row in range(HEIGHT) if all(board[row])]
    for row in full_rows:
        del board[row]
        board.insert(0, [0] * WIDTH)


# 游戏主循环
def game_loop():
    shape, x, y = generate_new_piece()
    game_over = False

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT and is_valid_move(shape, x - 1, y):
                    x -= 1
                elif event.key == pygame.K_RIGHT and is_valid_move(shape, x + 1, y):
                    x += 1
                elif event.key == pygame.K_DOWN and is_valid_move(shape, x, y + 1):
                    y += 1
                elif event.key == pygame.K_SPACE:
                    # 旋转方块
                    rotated_shape = [[shape[col][row] for col in range(len(shape))] for row in
                                     range(len(shape[0]) - 1, -1, -1)]
                    if is_valid_move(rotated_shape, x, y):
                        shape = rotated_shape

        if is_valid_move(shape, x, y + 1):
            y += 1
        else:
            merge_piece(shape, x, y)
            clear_lines()
            shape, x, y = generate_new_piece()
            if not is_valid_move(shape, x, y):
                game_over = True

        screen.fill(BLACK)
        for row in range(HEIGHT):
            for col in range(WIDTH):
                if board[row][col]:
                    pygame.draw.rect(screen, GREEN, (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

        for row in range(len(shape)):
            for col in range(len(shape[row])):
                if shape[row][col]:
                    pygame.draw.rect(screen, WHITE,
                                     ((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

        pygame.display.flip()
        clock.tick(1)


if __name__ == "__main__":
    game_loop()
    pygame.quit()
